'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  async userList() {
    const { ctx, service } = this;
    ctx.body = ctx.params;
    console.log('this.ctx.session',this.ctx.session);
    // // 1.增加数据
    // let addData = { username: "userName", password: "123456",sort:1 }
    // let addRes = await this.app.mysql.insert(
    //   "user", addData
    // );
    // // 2.查询单条数据 
    // let userData = await this.app.mysql.get(
    //   'user', { id: 1 }
    // );
    // // 3.查询多条数据，正序
    // let userDataAll = await this.app.mysql.select(
    //   'user', { orders: [['sort', 'asc']] }
    // );
    // // 4.修改
    // let updRes = await this.app.mysql.update('user',{ id:1, username:'test' });
    // // 5.删除
    // let delRes= await this.app.mysql.delete('user',{ id:1 });

    // ctx.body = userDataAll;
  }
  async add() {
    const { ctx } = this;

  }
  async login() {
    const { ctx } = this;
    const params = ctx.params;
    const token = await this.setToken({username: params.username});
    return ctx.returnBody(200, '登陆成功！', token);
  }
  async setToken(data) {
    const { ctx, app } = this;
    const token = app.jwt.sign(data, app.config.jwt.key);
    if (!token) return;
    if (ctx.session.tokenList) {
      ctx.session.tokenList.push(token);
    } else {
      ctx.session.tokenList = [token];
    }
    return token;
  }
}

module.exports = UserController;
